home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 6 / FM Towns Free Software Collection 6.iso / ms_dos / fplink / fplink.c next >
Encoding:
C/C++ Source or Header  |  1993-07-08  |  35.7 KB  |  1,524 lines

  1. /*    PC98<->FMR ファイル転送 for FMR/PC98 Copyright (c) E.Suto  , 1992-1993    */
  2. /*    修正履歴  Ver  0.00     1992/06/14     試作品                                    */
  3. /*              Ver  0.10     1992/06/15     高速化                                    */
  4. /*              Ver  0.20     1992/06/15     ワイルドカードサポート                    */
  5. /*              Ver  1.00     1992/06/16     後悔じゃなくて公開版                    */
  6. /*              Ver  1.10     1992/06/17     ファイル属性,日付及び時間の転送        */
  7. /*              Ver  1.11     1992/06/25     OS/2&共用ドライブ対応/¥バグ修正    */
  8. /*              Ver  1.12     1992/06/25     ちょっと高速化(^^;と相手待ちのESC停止    */
  9. /*              Ver  2.00     1992/06/25     簡易サーバーモード追加                    */
  10. /*              Ver  2.10     1992/06/26     簡易リモートコマンド追加                */
  11. /*              Ver  2.30     1992/07/01     TIMEOUT()改造,オプション統合,速度表示    */
  12. /*              Ver  2.31     1992/07/02     簡易サーバーを出来るだけ止めないよう    */
  13. /*              Ver  2.32     1992/07/03     速度表示の0割り算ガード(超手抜き)    */
  14. /*              Ver  2.33     1992/07/06     . の消し方変更                            */
  15. /*              Ver  2.34     1992/07/09     受信ファイルがルートに行ってしまうバグ修正    */
  16. /*              Ver  2.35     1992/11/17     RS232Cのバグ修正                    */
  17. /*              Ver  2.36     1992/11/17     ちょっと高速化(^^;                        */
  18. /*              Ver  2.37     1992/11/17     もうちょっと高速化(^^;;                */
  19. /*              Ver  2.38     1992/11/17     もうちょびっと高速化(^^;;;                */
  20. /*              Ver  2.39     1992/11/18     タイマ値最適化                            */
  21. /*              Ver  2.40     1992/11/18     送信ファイル名バグ修正                    */
  22. /*              Ver  2.41     1992/12/11     inpにwait-loop(;_;)                    */
  23. /*              Ver  2.42     1992/12/11     inpにwait-loop(;_;)の改良?でも止め(^^;*/
  24. /*              Ver  2.43     1993/ 2/ 2     関数を変数で呼び出す                    */
  25. /*              Ver  2.44     1993/ 2/ 3     失敗時にタイムアウトしない                */
  26. /*              Ver  2.45     1993/ 2/ 4     コマンド受信処理でタイムアウトしない    */
  27.  
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <dos.h>
  31. #include <io.h>
  32. #include <fcntl.h>
  33. #include <signal.h>
  34. #include <string.h>
  35. #include <time.h>
  36. #include <sys\timeb.h>
  37.  
  38. #define BUFFSIZE    32767
  39. #define PALAWAIT    0    /* TIMEOUTが発生する場合はこの値を大きくしてね(^^; */
  40.  
  41. void    copyright() ;
  42. int        connect_r() ;
  43. int        connect_s() ;
  44. void    fp_command() ;
  45. void    fp_file() ;
  46. void    fp_path() ;
  47. void    fp_send() ;
  48. void    fp_server() ;
  49. void    fp_receive() ;
  50. void    fp_receive0() ;    /* (^^;;;;    */
  51. void    m_check() ;
  52. void    m_set() ;
  53. int        (*receive_)( int , unsigned char * ) ;
  54. int        receive_f( int , unsigned char * ) ;
  55. int        receive_p( int , unsigned char * ) ;
  56. void    rs_init() ;
  57. int        (*send_)( int , unsigned char * ) ;
  58. int        send_f( int , unsigned char * ) ;
  59. int        send_p( int , unsigned char * ) ;
  60. void    sig_out() ;
  61. void    timeout() ;
  62. void    timeset() ;
  63. void    usage() ;
  64.  
  65. static    int                machine        ; /* 機種  0:FMR 1:PC98                    */
  66. static    int                mode        ; /* 動作モード 0:S 1:R 2:サ 3:サS 4:サR 5:*/
  67. static    unsigned int    rs_in        ; /* RS_232Cステータスアドレス            */
  68. static    unsigned int    rs_out        ; /* RS_232Cコマンドアドレス            */
  69. static    unsigned char    rs_rev        ; /* RS_232Cステータス反転マスク値        */
  70. static    unsigned char    rs_mask        ; /* RS_232Cステータスマスク値            */
  71. static    unsigned char    rs_ci        ; /* RS_232CCI_ONチェック値        */
  72. static    unsigned char    rs_cd        ; /* RS_232CCD_ONチェック値        */
  73. static    unsigned char    rs_cics        ; /* RS_232CCS/CI    ON            */
  74. static    unsigned char    name[128]    ; /* 操作ファイル名                        */
  75. static    unsigned char    path[128]    ; /* 送信先パス名                        */
  76. static    unsigned char    command[128]; /* 送信コマンド                        */
  77. static    unsigned char    buffer[BUFFSIZE];/* バッファ                        */
  78. struct    find_t            f_name        ; /* ワイルドカード展開データ            */
  79. struct    timeb            time_1        ; /* 速度計算用タイマ値1                */
  80. struct    timeb            time_2        ; /* 速度計算用タイマ値2                */
  81. static    long            timeout1    ; /* タイムアウト値                        */
  82. static    int                debug        ; /* 詳細表示                            */
  83.  
  84.  
  85. void main(int argc,char *argv[])
  86. {
  87.     int i , k , l , m ;
  88.     unsigned char *c ;
  89.  
  90.     debug    = 0 ;
  91.     mode    = -1 ;
  92.     machine = -1 ;
  93.     strcpy( path , "" ) ;
  94.     strcpy( command , "" ) ;
  95.  
  96. /* コピーライト表示 */
  97.     copyright() ;
  98.  
  99. /* 割り込み処理定義 */
  100.     signal( SIGINT , sig_out ) ;
  101.  
  102. /* オプションチェック */
  103.     if( argc < 2 ) {
  104.         usage() ;
  105.         exit( 0 ) ;
  106.     }
  107.     else {
  108.         k = l = 0 ;
  109.         for( i=1 ; i < argc ; i++ ) {
  110.             c = argv[i] ;
  111.             if( *c == '-' || *c == '/' ) {
  112.                 switch( *++c ) {
  113.                 case '?' :
  114.                     usage();
  115.                     exit( 0 );
  116.                 case 'i' :
  117.                 case 'I' :
  118.                     debug = -1 ;
  119.                     break ;
  120.                 case 'R' :
  121.                 case 'r' :
  122.                     if( k ) mode = 4 ;
  123.                     else mode = 1 ;
  124.                     break ;
  125.                 case 'S' :
  126.                 case 's' :
  127.                     mode = 0 ;
  128.                     break ;
  129.                 case 'Z' :
  130.                 case 'z' :
  131.                     mode = 2 ;
  132.                     break ;
  133.                 case 'C' :
  134.                 case 'c' :
  135.                     mode = 5 ;
  136.                     c++ ;
  137.                     strcpy( command , c ) ;
  138.                     if( command[0] == '"' ) {
  139.                         for( ; i < argc ; ) {
  140.                             i++ ;
  141.                             strcat( command , " " ) ;
  142.                             strcat( command , argv[i] ) ;
  143.                             if( command[strlen( command ) - 1 ] == '"' ) 
  144.                                 break ;
  145.                         }
  146.                     }
  147.                     break ;
  148.                 case 'X' :
  149.                 case 'x' :
  150.                     c++ ;
  151.                     strcpy( path , c ) ;
  152.                     l = -1 ;
  153.                     break ;
  154.                 case 'P' :
  155.                 case 'p' :
  156.                     machine = 1 ;
  157.                     break ;
  158.                 case 'F' :
  159.                 case 'f' :
  160.                     machine = 0 ;
  161.                     break ;
  162.                 default :
  163. perr:                    printf( "ファイル名が2つ以上指定されているか、誤ったパラメタ(%s)が指定されています。\n" , argv[i] ) ;
  164.                     usage() ;
  165.                     exit( -1 ) ;
  166.                 }
  167.             }
  168.             else {
  169.                 if( mode == 5 ) {
  170.                     if( strlen( command ) > 0 ) goto perr ;
  171.                     strcpy( command , c ) ;
  172.                     if( command[0] == '"' ) {
  173.                         for( ; i < argc ; ) {
  174.                             i++ ;
  175.                             strcat( command , " " ) ;
  176.                             strcat( command , argv[i] ) ;
  177.                             if( command[strlen( command ) - 1 ] == '"' ) 
  178.                                 break ;
  179.                         }
  180.                     }
  181.                     else {
  182.                         for( ; i < argc ; ) {
  183.                             i++ ;
  184.                             if( ( *(argv[i]) == '-' ) || ( *(argv[i]) == '/' ) ) 
  185.                                 break ;
  186.                             strcat( command , " " ) ;
  187.                             strcat( command , argv[i] ) ;
  188.                         }
  189.                     }
  190.                 }
  191.                 else {
  192.                     if( k ) goto perr ;
  193.                     strcpy( name , argv[i] ) ;
  194.                     k = -1 ;
  195.                     if( mode == 1 ) mode = 4 ;
  196.                 }
  197.             }
  198.         }
  199.     }
  200.  
  201. /* オプションの関連チェック */
  202.     if( mode == -1 ) {
  203.         printf( "-r,-s,-z又は-c,の何れかを指定してください。\n" ) ;
  204.         usage() ;
  205.         exit( -1 ) ;
  206.     }
  207.     if( !( k ) && ( mode == 0 ) ) {
  208.         printf( "-s指定時にはファイル名を省略できません。\n" ) ;
  209.         usage() ;
  210.         exit( -1 ) ;
  211.     }
  212.     if( l && ( mode != 0 ) ) {
  213.         printf( "-s 指定時以外にはでは、-xは指定できません。\n" ) ;
  214.         usage() ;
  215.         exit( -1 ) ;
  216.     }
  217.     if( mode == 5 ) {
  218.         strcpy( buffer , command ) ;
  219.         if( buffer[0] == '"' ) {
  220.             m = strlen( buffer ) ;
  221.             if( buffer[m-1] != '"' ) {
  222.                 printf( "コマンドの終わりを示す\"が不足しています。(%s)\n",buffer ) ;
  223.                 usage() ;
  224.                 exit( -1 ) ;
  225.             }
  226.             buffer[m-1] = 0x00 ;
  227.             strcpy( command , ( buffer + 1 ) ) ;
  228.         }
  229.         if( strlen( command ) == 0 ) {
  230.             printf( "-c に続いてリモートコマンドを指定してください。\n" ) ;
  231.             usage() ;
  232.             exit( -1 ) ;
  233.         }
  234.         if( strlen( command ) > 114 ) {
  235.             printf( "申し訳有りませんがコマンドが長すぎます。114文字以下にしてください。\n" ) ;
  236.             usage() ;
  237.             exit( -1 ) ;
  238.         }
  239.     }
  240.  
  241. /* 機種判定 */
  242.     if( machine == -1 ) m_check() ;
  243.     m_set() ;
  244.  
  245. /* タイムアウト値設定 */
  246.     timeset() ;
  247.  
  248. /* 回線の初期化 */
  249.     rs_init() ;
  250.  
  251. /* 送受信処理へ */
  252.     switch( mode ) {
  253.         case 0 :                /* 送信処理 */
  254.             connect_s() ;
  255.             fp_send() ;
  256.             break ;
  257.         case 1 :                /* 受信処理 */
  258.             connect_r() ;
  259.             fp_receive0() ;
  260.             fp_receive() ;
  261.             break ;
  262.         case 2 :                /* サーバー処理 */
  263.             fp_server() ;
  264.             break ;
  265.         case 4 :                /* サーバーからの受信処理 */
  266.             connect_s() ;
  267.             fp_file() ;
  268.             fp_receive0() ;
  269.             fp_receive() ;
  270.             break ;
  271.         case 5 :                /* リモートコマンド処理 */
  272.             connect_s() ;
  273.             fp_command() ;
  274.             break ;
  275.     }
  276.  
  277. /* 後始末 */
  278.     printf( "電送処理終了しました。\n" ) ;
  279.     rs_init() ;
  280.  
  281. }
  282.  
  283. /* 手抜き機種判定(まねしない方が良いですよ(^^;) */
  284. void m_check()
  285. {
  286.     machine = -1 ;
  287.  
  288. /* PC98の判定 */
  289.  
  290.     if( ( inp( 0x0032 ) & 0x3e ) == 0x04 ) {
  291.         machine = 1 ;                        /* PC98かな?    */
  292.     }
  293.     else if( inp( 0x0a02 ) != 0xff ) {
  294.         machine = 0 ;                        /* FMRかな?        */
  295.     }
  296.  
  297.     if(  machine == -1 ) {
  298.         printf( "ごめんなさい機種がわからないの(;_;) -f か -p を指定してね。\n" ) ;
  299.         exit( -1 ) ;
  300.     }
  301.  
  302.     if( debug ) {
  303.         if( machine ) printf( "たぶんPC98と思う。\n" ) ;
  304.         else printf( "たぶんFMRと思う。\n" ) ;
  305.     }
  306.  
  307. }
  308.  
  309. /* 機種情報設定 */
  310. void m_set()
  311. {
  312.  
  313. /* PC98の設定 */
  314.     if( machine ) {
  315.         rs_in  = 0x0033 ;
  316.         rs_out = 0x0032 ;
  317.         rs_rev     = 0xff ;
  318.         rs_mask     = 0xe0 ;
  319.         rs_cd     = 0x20 ;
  320.         rs_ci     = 0x80 ;
  321.         rs_cics     = 0xc0 ;
  322.         send_    = send_p ;
  323.         receive_ = receive_p ;
  324.     }
  325.  
  326. /* FMRの設定 */
  327.     else {
  328.         rs_in     = 0x0a04 ;
  329.         rs_out     = 0x0a02 ;
  330.         rs_rev     = 0x00 ;
  331.         rs_mask     = 0x07 ;
  332.         rs_cd     = 0x04 ;
  333.         rs_ci     = 0x01 ;
  334.         rs_cics     = 0x03 ;
  335.         send_    = send_f ;
  336.         receive_ = receive_f ;
  337.     }
  338.  
  339. }
  340.  
  341. /* 8251の初期化 */
  342. void rs_init()
  343. {
  344.     int i , j ;
  345.  
  346. /* 状態のリセット */
  347.     for( i = 0 ; i < 3 ; i++ ) {
  348.         outp( rs_out , 0x00 ) ;
  349.         for( j = 0 ; j < 3 ; j++ ) ; /* 念のため時間待ち */
  350.     }
  351.     outp( rs_out , 0x40) ;
  352.  
  353. /* モードセット */
  354.     outp( rs_out , 0x4e) ;
  355.     for( j = 0 ; j < 6 ; j++ ) ; /* 念のため時間待ち */
  356.  
  357. /* エラーリセット/コマンドセット */
  358.     outp( rs_out , 0x37) ;
  359.     for( j = 0 ; j < 6 ; j++ ) ; /* 念のため時間待ち */
  360.  
  361. }
  362.  
  363. /* 接続確認待ち(送信型)*/
  364. int connect_s()
  365. {
  366.     unsigned char stat ;
  367.  
  368. /* 接続確認 */
  369.     if( mode != 2 ) printf( "接続待ちです。\n" ) ;
  370.     outp( rs_out , 0x02 ) ;        /* ずーと待つからsendは使わない        */
  371.     stat = rs_ci ;
  372.     while( ( ( inp( rs_in ) ^ rs_rev ) & rs_mask ) != stat ) {
  373.         printf( "*\r" ) ;
  374.         if( kbhit() ) {
  375.             if( getch() == 0x1b ) sig_out() ;
  376.         }
  377.     }
  378.  
  379.     outp( rs_out , 0x2a ) ;        /* ずーと待つからsendは使わない        */
  380.     stat = rs_cd | rs_cics ;
  381.     while( ( ( inp( rs_in ) ^ rs_rev ) & rs_mask ) != stat ) {
  382.         printf( "*\r" ) ;
  383.         if( kbhit() ) {
  384.             if( getch() == 0x1b ) sig_out() ;
  385.         }
  386.     }
  387.  
  388.     buffer[0] = 0xe4 ;            /* このへんの文字は何でも良い(^^;    */
  389.     if( send_( 1 , buffer ) ) {
  390.         if( mode == 2 ) return( -1 ) ;
  391.         else timeout() ;
  392.     }
  393.     if( debug ) printf( "送信型接続OK。\n" ) ;
  394.     return( 0 ) ;
  395.  
  396. }
  397.  
  398. /* 接続確認待ち(受信型)*/
  399. int connect_r()
  400. {
  401.     unsigned char stat ;
  402.  
  403. /* 接続確認 */
  404.     if( mode != 2 ) printf( "接続待ちです。\n" ) ;
  405.     stat = rs_ci ;
  406.     while( ( ( inp( rs_in ) ^ rs_rev ) & rs_mask ) != stat ) {
  407.         printf( "*\r" ) ;
  408.         if( kbhit() ) {
  409.             if( getch() == 0x1b ) sig_out() ;
  410.         }
  411.     }
  412.     outp( rs_out , 0x02 ) ;        /* ずっと待ちたいからreceiveを使わない    */
  413.  
  414.     stat = rs_cd | rs_cics ;
  415.     while( ( ( inp( rs_in ) ^ rs_rev ) & rs_mask ) != stat ) {
  416.         printf( "*\r" ) ;
  417.         if( kbhit() ) {
  418.             if( getch() == 0x1b ) sig_out() ;
  419.         }
  420.     }
  421.     outp( rs_out , 0x2a ) ;        /* ずっと待ちたいからreceiveを使わない    */
  422.  
  423.     if( receive_( 1 , buffer ) ) {
  424.         if( mode == 2 ) return( -1 ) ;
  425.         timeout() ;
  426.     }
  427.     if( buffer[0] != 0xe4 ) {    /* このへんの文字は送信側に合わせる        */
  428.         printf( "接続確認処理でエラーが発生しました。\n" ) ;
  429.         if( mode == 2 ) return( -1 ) ;
  430.         rs_init() ;
  431.         exit( -1 ) ;
  432.     }
  433.     if( debug ) printf( "受信型接続OK。\n" ) ;
  434.     return( 0 ) ;
  435.  
  436. }
  437.  
  438. /* 回線送信処理 Fj */
  439. int send_f( int l , unsigned char *b )
  440. {
  441.     int i ;
  442.     long k ;
  443.     unsigned char stat0,stat1,data ;
  444.  
  445. /* 1バイトを4つに区切って2ビット毎に送信 */
  446.     for( i = 0 ; i < l ; i++ ) {
  447. /* ビット7ビット6送信 */
  448.         data = stat0 = 0x00 ;
  449.         switch( (*b) & 0xc0 ) {
  450.         case 0xc0 :
  451.             data |= 0x22 ;
  452.             stat0 |= 0x03 ;
  453.             break ;
  454.         case 0x80 :
  455.             data |= 0x20 ;
  456.             stat0 |= 0x02 ;
  457.             break ;
  458.         case 0x40 :
  459.             data |= 0x02 ;
  460.             stat0 |= 0x01 ;
  461.             break ;
  462.         }
  463.         outp( 0x0a02 , data ) ;
  464. /* ビット5ビット4送信 */
  465.         data = 0x08 ;
  466.         stat1 = 0x04 ;
  467.         switch( (*b) & 0x30 ) {
  468.         case 0x30 :
  469.             data |= 0x22 ;
  470.             stat1 |= 0x03 ;
  471.             break ;
  472.         case 0x20 :
  473.             data |= 0x20 ;
  474.             stat1 |= 0x02 ;
  475.             break ;
  476.         case 0x10 :
  477.             data |= 0x02 ;
  478.             stat1 |= 0x01 ;
  479.             break ;
  480.         }
  481.         for( k = timeout1 ; ( ( inp( 0x0a04 )  ) & 0x07 ) != stat0
  482.                 ; k-- ) {
  483.             if( !( k ) ) return( -1 ) ;
  484.         }
  485.         outp( 0x0a02 , data ) ;
  486. /* ビット3ビット2送信 */
  487.         data = stat0 = 0x00 ;
  488.         switch( (*b) & 0x0c ) {
  489.         case 0x0c :
  490.             data |= 0x22 ;
  491.             stat0 |= 0x03 ;
  492.             break ;
  493.         case 0x08 :
  494.             data |= 0x20 ;
  495.             stat0 |= 0x02 ;
  496.             break ;
  497.         case 0x04 :
  498.             data |= 0x02 ;
  499.             stat0 |= 0x01 ;
  500.             break ;
  501.         }
  502.         for( k = timeout1 ; ( ( inp( 0x0a04 )  ) & 0x07 ) != stat1
  503.                 ; k-- ) {
  504.             if( !( k ) ) return( -1 ) ;
  505.         }
  506.         outp( 0x0a02 , data ) ;
  507. /* ビット1ビット0送信 */
  508.         data = 0x08 ;
  509.         stat1 = 0x04 ;
  510.         switch( (*b) & 0x03 ) {
  511.         case 0x03 :
  512.             data |= 0x22 ;
  513.             stat1 |= 0x03 ;
  514.             break ;
  515.         case 0x02 :
  516.             data |= 0x20 ;
  517.             stat1 |= 0x02 ;
  518.             break ;
  519.         case 0x01 :
  520.             data |= 0x02 ;
  521.             stat1 |= 0x01 ;
  522.             break ;
  523.         }
  524.         for( k = timeout1 ; ( ( inp( 0x0a04 )  ) & 0x07 ) != stat0
  525.                 ; k-- ) {
  526.             if( !( k ) ) return( -1 ) ;
  527.         }
  528.         outp( 0x0a02 , data ) ;
  529.         for( k = timeout1 ; ( ( inp( 0x0a04 )  ) & 0x07 ) != stat1
  530.                 ; k-- ) {
  531.             if( !( k ) ) return( -1 ) ;
  532.         }
  533. /* 次のバイト処理へ */
  534.         b++ ;
  535.     }
  536.     return( 0 ) ;
  537.  
  538. }
  539.  
  540. /* 回線送信処理 NEC */
  541. int send_p( int l , unsigned char *b )
  542. {
  543.     int i ;
  544.     long k ;
  545.     unsigned char stat0,stat1,data ;
  546.  
  547. /* 1バイトを4つに区切って2ビット毎に送信 */
  548.     for( i = 0 ; i < l ; i++ ) {
  549. /* ビット7ビット6送信 */
  550.         data = stat0 = 0x00 ;
  551.         switch( (*b) & 0xc0 ) {
  552.         case 0xc0 :
  553.             data |= 0x22 ;
  554.             stat0 |= 0xc0 ;
  555.             break ;
  556.         case 0x80 :
  557.             data |= 0x20 ;
  558.             stat0 |= 0x40 ;
  559.             break ;
  560.         case 0x40 :
  561.             data |= 0x02 ;
  562.             stat0 |= 0x80 ;
  563.             break ;
  564.         }
  565.         outp( 0x0032 , data ) ;
  566. /* ビット5ビット4送信 */
  567.         data = 0x08 ;
  568.         stat1 = 0x20 ;
  569.         switch( (*b) & 0x30 ) {
  570.         case 0x30 :
  571.             data |= 0x22 ;
  572.             stat1 |= 0xc0 ;
  573.             break ;
  574.         case 0x20 :
  575.             data |= 0x20 ;
  576.             stat1 |= 0x40 ;
  577.             break ;
  578.         case 0x10 :
  579.             data |= 0x02 ;
  580.             stat1 |= 0x80 ;
  581.             break ;
  582.         }
  583.         for( k = timeout1 ; ( ( inp( 0x0033 ) ^ 0xff ) & 0xe0 ) != stat0
  584.                 ; k-- ) {
  585.             if( !( k ) ) return( -1 ) ;
  586.         }
  587.         outp( 0x0032 , data ) ;
  588. /* ビット3ビット2送信 */
  589.         data = stat0 = 0x00 ;
  590.         switch( (*b) & 0x0c ) {
  591.         case 0x0c :
  592.             data |= 0x22 ;
  593.             stat0 |= 0xc0 ;
  594.             break ;
  595.         case 0x08 :
  596.             data |= 0x20 ;
  597.             stat0 |= 0x40 ;
  598.             break ;
  599.         case 0x04 :
  600.             data |= 0x02 ;
  601.             stat0 |= 0x80 ;
  602.             break ;
  603.         }
  604.         for( k = timeout1 ; ( ( inp( 0x0033 ) ^ 0xff ) & 0xe0 ) != stat1
  605.                 ; k-- ) {
  606.             if( !( k ) ) return( -1 ) ;
  607.         }
  608.         outp( 0x0032 , data ) ;
  609. /* ビット1ビット0送信 */
  610.         data = 0x08 ;
  611.         stat1 = 0x20 ;
  612.         switch( (*b) & 0x03 ) {
  613.         case 0x03 :
  614.             data |= 0x22 ;
  615.             stat1 |= 0xc0 ;
  616.             break ;
  617.         case 0x02 :
  618.             data |= 0x20 ;
  619.             stat1 |= 0x40 ;
  620.             break ;
  621.         case 0x01 :
  622.             data |= 0x02 ;
  623.             stat1 |= 0x80 ;
  624.             break ;
  625.         }
  626.         for( k = timeout1 ; ( ( inp( 0x0033 ) ^ 0xff ) & 0xe0 ) != stat0
  627.                 ; k-- ) {
  628.             if( !( k ) ) return( -1 ) ;
  629.         }
  630.         outp( 0x0032 , data ) ;
  631.         for( k = timeout1 ; ( ( inp( 0x0033 ) ^ 0xff ) & 0xe0 ) != stat1
  632.                 ; k-- ) {
  633.             if( !( k ) ) return( -1 ) ;
  634.         }
  635. /* 次のバイト処理へ */
  636.         b++ ;
  637.     }
  638.     return( 0 ) ;
  639.  
  640. }
  641.  
  642. /* 回線受信処理 Fj */
  643. int receive_f( int l , unsigned char *b )
  644. {
  645.     int i , j ;
  646.     long k ;
  647.     unsigned char stat,data ;
  648.  
  649. /* 2ビットを4つ受信で1バイト */
  650.     for( i = 0 ; i < l ; i++ ) {
  651.         *b = 0x00 ;
  652. /* ビット7ビット6受信 */
  653.         data = 0x00 ;
  654.         for( k = timeout1 ; ( inp( 0x0a04 )  ) & 0x04 ; k-- ) {
  655.             if( !( k ) ) return( -1 ) ;
  656.         }
  657.         for( j = 0 ; j < PALAWAIT ; j++ ) ;
  658.         stat = inp( 0x0a04 )  ;
  659.         if( stat & 0x02 ) {
  660.                 (*b) |= 0x80 ;
  661.             data |= 0x20 ;
  662.         }
  663.         if( stat & 0x01 ) {
  664.                 (*b) |= 0x40 ;
  665.             data |= 0x02 ;
  666.         }
  667.         outp( 0x0a02 , data ) ;
  668. /* ビット5ビット4受信 */
  669.         data = 0x08 ;
  670.         for( k = timeout1 ; !( ( inp( 0x0a04 )  ) & 0x04 ) ; k-- ) {
  671.             if( !( k ) ) return( -1 ) ;
  672.         }
  673.             for( j = 0 ; j < PALAWAIT ; j++ ) ;
  674.             stat = inp( 0x0a04 )  ;
  675.         if( stat & 0x02 ) {
  676.                 (*b) |= 0x20 ;
  677.             data |= 0x20 ;
  678.         }
  679.         if( stat & 0x01 ) {
  680.                 (*b) |= 0x10 ;
  681.             data |= 0x02 ;
  682.         }
  683.         outp( 0x0a02 , data ) ;
  684. /* ビット3ビット2受信 */
  685.         data = 0x00 ;
  686.         for( k = timeout1 ; ( inp( 0x0a04 )  ) & 0x04 ; k-- ) {
  687.             if( !( k ) ) return( -1 ) ;
  688.         }
  689.         for( j = 0 ; j < PALAWAIT ; j++ ) ;
  690.         stat = inp( 0x0a04 )  ;
  691.         if( stat & 0x02 ) {
  692.                 (*b) |= 0x08 ;
  693.             data |= 0x20 ;
  694.         }
  695.         if( stat & 0x01 ) {
  696.                 (*b) |= 0x04 ;
  697.             data |= 0x02 ;
  698.         }
  699.         outp( 0x0a02 , data ) ;
  700. /* ビット1ビット0受信 */
  701.         data = 0x08 ;
  702.         for( k = timeout1 ; !( ( inp( 0x0a04 )  ) & 0x04 ) ; k-- ) {
  703.             if( !( k ) ) return( -1 ) ;
  704.         }
  705.         for( j = 0 ; j < PALAWAIT ; j++ ) ;
  706.         stat = inp( 0x0a04 )  ;
  707.         if( stat & 0x02 ) {
  708.                 (*b) |= 0x02 ;
  709.             data |= 0x20 ;
  710.         }
  711.         if( stat & 0x01 ) {
  712.                 (*b) |= 0x01 ;
  713.             data |= 0x02 ;
  714.         }
  715.         outp( 0x0a02 , data ) ;
  716. /* 次のバイト処理へ */
  717.         b++ ;
  718.     }
  719.     return( 0 ) ;
  720.  
  721. }
  722.  
  723. /* 回線受信処理 NEC */
  724. int receive_p( int l , unsigned char *b )
  725. {
  726.     int i , j ;
  727.     long k ;
  728.     unsigned char stat,data ;
  729.  
  730. /* 2ビットを4つ受信で1バイト */
  731.     for( i = 0 ; i < l ; i++ ) {
  732.         *b = 0x00 ;
  733. /* ビット7ビット6受信 */
  734.         data = 0x00 ;
  735.         for( k = timeout1 ; ( inp( 0x0033 ) ^ 0xff ) & 0x20 ; k-- ) {
  736.             if( !( k ) ) return( -1 ) ;
  737.         }
  738.         for( j = 0 ; j < PALAWAIT ; j++ ) ;
  739.         stat = inp( 0x0033 ) ^ 0xff ;
  740.         if( stat & 0x40 ) {
  741.                 (*b) |= 0x80 ;
  742.             data |= 0x20 ;
  743.         }
  744.         if( stat & 0x80 ) {
  745.                 (*b) |= 0x40 ;
  746.             data |= 0x02 ;
  747.         }
  748.         outp( 0x0032 , data ) ;
  749. /* ビット5ビット4受信 */
  750.         data = 0x08 ;
  751.         for( k = timeout1 ; !( ( inp( 0x0033 ) ^ 0xff ) & 0x20 ) ; k-- ) {
  752.             if( !( k ) ) return( -1 ) ;
  753.         }
  754.         for( j = 0 ; j < PALAWAIT ; j++ ) ;
  755.         stat = inp( 0x0033 ) ^ 0xff ;
  756.         if( stat & 0x40 ) {
  757.                 (*b) |= 0x20 ;
  758.             data |= 0x20 ;
  759.         }
  760.         if( stat & 0x80 ) {
  761.                 (*b) |= 0x10 ;
  762.             data |= 0x02 ;
  763.         }
  764.         outp( 0x0032 , data ) ;
  765. /* ビット3ビット2受信 */
  766.         data = 0x00 ;
  767.         for( k = timeout1 ; ( inp( 0x0033 ) ^ 0xff ) & 0x20 ; k-- ) {
  768.             if( !( k ) ) return( -1 ) ;
  769.         }
  770.         for( j = 0 ; j < PALAWAIT ; j++ ) ;
  771.         stat = inp( 0x0033 ) ^ 0xff ;
  772.         if( stat & 0x40 ) {
  773.                 (*b) |= 0x08 ;
  774.             data |= 0x20 ;
  775.         }
  776.         if( stat & 0x80 ) {
  777.                 (*b) |= 0x04 ;
  778.             data |= 0x02 ;
  779.         }
  780.         outp( 0x0032 , data ) ;
  781. /* ビット1ビット0受信 */
  782.         data = 0x08 ;
  783.         for( k = timeout1 ; !( ( inp( 0x0033 ) ^ 0xff ) & 0x20 ) ; k-- ) {
  784.             if( !( k ) ) return( -1 ) ;
  785.         }
  786.         for( j = 0 ; j < PALAWAIT ; j++ ) ;
  787.         stat = inp( 0x0033 ) ^ 0xff ;
  788.         if( stat & 0x40 ) {
  789.                 (*b) |= 0x02 ;
  790.             data |= 0x20 ;
  791.         }
  792.         if( stat & 0x80 ) {
  793.                 (*b) |= 0x01 ;
  794.             data |= 0x02 ;
  795.         }
  796.         outp( 0x0032 , data ) ;
  797. /* 次のバイト処理へ */
  798.         b++ ;
  799.     }
  800.     return( 0 ) ;
  801.  
  802. }
  803.  
  804. /* コピーライト表示 */
  805. void copyright()
  806. {
  807.     printf(
  808.         "FMR <=> PC98 ファイル電送 Ver 2.45, Copyright (C) E.Suto , 1992-1993\n"
  809.         ) ;
  810. }
  811.  
  812. /* 使い方表示 */
  813. void usage()
  814. {
  815.     printf( "使用方法                : FPLINK [options] [file-name]\n" ) ;
  816.     printf( "オプション -s            : ファイル送信(要ファイル名)\n" ) ;
  817.     printf( "      -r            : ファイル受信\n" ) ;
  818.     printf( "      -z            : サーバーモード\n" ) ;
  819.     printf( "      -cリモートコマンド  : サーバーにリモートコマンドを送信\n" ) ;
  820.     printf( "      -x[path_name] : -sでの格納先パス名\n" ) ;
  821.     printf( "          -f            : 強制FMR指定\n" ) ;
  822.     printf( "          -p            : 強制PC98指定\n" ) ;
  823.     printf( "          -i            : 詳細情報表示\n" ) ;
  824. }
  825.  
  826. /*---------------------------------------------------------------------------*/
  827.  
  828. /* 送信メイン処理 */
  829. void fp_send()
  830. {
  831.     int len , loop , i , err ;
  832.     unsigned long f_len ;
  833.     unsigned char *c , *c2 ;
  834.     unsigned char path_name[128] , full_name[128] ;
  835.     FILE *file ;
  836.     int handle ;
  837.     unsigned long speed , f_len_b ;
  838.  
  839. /* ファイル送信前フラグ送信(^^; */
  840.     buffer[0] = 0x02 ;
  841.     if( send_( 1 , buffer ) ) {
  842.         if( mode == 2 ) return ;
  843.         timeout() ;
  844.     }
  845.     if( debug ) printf( "ファイル送信フラグ送信OK。\n" ) ;
  846.  
  847. /* ファイル送信フラグ送信(^^; */
  848.     buffer[0] = 0x00 ;
  849.     if( send_( 1 , buffer ) ) {
  850.         if( mode == 2 ) return ;
  851.         timeout() ;
  852.     }
  853.     if( debug ) printf( "ファイル送信フラグ送信OK。\n" ) ;
  854.  
  855. /* 受信先パス名長送信 */
  856.     len = strlen( path ) ;
  857.     *buffer = (unsigned char)len ;
  858.     if( send_( 1 , buffer ) ) {
  859.         if( mode == 2 ) return ;
  860.         timeout() ;
  861.     }
  862.     if( debug ) printf( "受信先パス名長送信OK。\n" ) ;
  863.  
  864. /* 受信先パス名送信 */
  865.     if( len ) {
  866.         if( send_( len , path ) ) {
  867.             if( mode == 2 ) return ;
  868.             timeout() ;
  869.         }
  870.         if( debug ) printf( "受信先パス名送信OK。\n" ) ;
  871.     }
  872.  
  873. /* PATH名取り出し */
  874.     strcpy( path_name , name ) ;
  875.     if( ( c = strrchr( path_name , ':' ) ) == NULL ) {
  876.         c = path_name ;
  877.     }
  878.     if( ( c2 = strrchr( c , '\\' ) ) == NULL ) {
  879.         c2 = c ;
  880.     }
  881.     if( *c2 == ':' || *c2 == '\\' ) *(c2+1) = 0x00 ;
  882.     else strcpy( path_name , "" ) ;
  883.  
  884. /* ワイルドカード展開 */
  885.     if( ( _dos_findfirst( name , _A_ARCH|_A_RDONLY , &f_name ) ) != 0 ) {
  886.         printf( "送信出来るファイルがありません。\n" ) ;
  887.         buffer[0] = 0x00 ;
  888.         if( send_( 1 , buffer ) ) {
  889.             if( mode == 2 ) return ;
  890.             timeout() ;
  891.         }
  892.         if( mode == 2 ) return ;
  893.         rs_init() ;
  894.         exit( -1 ) ;
  895.     }
  896. /* 送るデータ有り */
  897.     buffer[0] = 0xff ;
  898.     if( send_( 1 , buffer ) ) {
  899.         if( mode == 2 ) return ;
  900.         timeout() ;
  901.     }
  902.  
  903. /* 送信処理ループ */
  904.     if( mode != 2 ) printf( " ファイル名    : ファイル長\n" ) ;
  905.     for( ; ; ) {
  906.         ftime( &time_1 ) ;
  907.         err = 0 ;
  908.  
  909. /* ファイルの長さを調べる _dos_finedではDIETしてあるデータの長さが分からない */
  910.         strcpy( full_name , path_name ) ;
  911.         strcat( full_name , f_name.name ) ;
  912.         if( ( handle = open( full_name , O_RDONLY|O_BINARY ) ) == EOF ) {
  913.             printf( "送信ファイルのオープンに失敗しました(%s)。\n" ,
  914.                 full_name ) ;
  915.             buffer[0] = 0x00 ;
  916.             if( send_( 1 , buffer ) ) {
  917.                 if( mode == 2 ) return ;
  918.                 timeout() ;
  919.             }
  920.             if( mode == 2 ) return ;
  921.             rs_init() ;
  922.             exit( -1 ) ;
  923.         }
  924.         f_len = filelength( handle ) ;
  925.         f_len_b = f_len * 8l ;
  926.         close( handle ) ;
  927.  
  928. /* ファイルのオープン */
  929.         if( ( file = fopen( full_name , "rb" ) ) == NULL ) {
  930.             printf( "送信ファイルのオープンに失敗しました。\n" ) ;
  931.             buffer[0] = 0x00 ;
  932.             if( send_( 1 , buffer ) ) {
  933.                 if( mode == 2 ) return ;
  934.                 timeout() ;
  935.             }
  936.             if( mode == 2 ) return ;
  937.             rs_init() ;
  938.             exit( -1 ) ;
  939.         }
  940.  
  941. /* 送信データのオープンOK */
  942.     buffer[0] = 0xff ;
  943.     if( send_( 1 , buffer ) ) {
  944.         if( mode == 2 ) return ;
  945.         timeout() ;
  946.     }
  947.  
  948. /* ファイル名長送信 */
  949.         len = strlen( f_name.name ) ;
  950.         *buffer = (unsigned char)len ;
  951.         if( send_( 1 , buffer ) ) {
  952.             if( mode == 2 ) return ;
  953.             timeout() ;
  954.         }
  955.         if( debug ) printf( "ファイル名長送信OK。\n" ) ;
  956.  
  957. /* ファイル名送信 */
  958.         strcpy( name , f_name.name ) ;
  959.         if( send_( len , f_name.name ) ) {
  960.             if( mode == 2 ) return ;
  961.             timeout() ;
  962.         }
  963.         if( debug ) printf( "ファイル名送信OK。\n" ) ;
  964.  
  965. /* 受信側ファイルチェック */
  966.     if( receive_( 1 , buffer ) ) {
  967.         if( mode == 2 ) return ;
  968.         timeout() ;
  969.     }
  970.     if( buffer[0] == 0x00 ) {
  971.         printf( "受信側でファイルのオープンに失敗しました。\n" ) ;
  972.         if( mode == 2 ) return ;
  973.         rs_init() ;
  974.         exit( -1 ) ;
  975.     }
  976.  
  977. /* ファイル長送信 */
  978.         buffer[0] = f_len / 0x1000000l ;
  979.         buffer[1] = ( f_len & 0x00ff0000l ) / 0x10000l ;
  980.         buffer[2] = ( f_len & 0x0000ff00l ) / 0x100l ;
  981.         buffer[3] = f_len & 0x000000ffl ;
  982.         if( send_( 4 , buffer ) ) {
  983.             if( mode == 2 ) return ;
  984.             timeout() ;
  985.         }
  986.         if( debug ) printf( "ファイル長送信OK。\n" ) ;
  987.  
  988. /* ファイル日付送信 */
  989.         buffer[0] = f_name.wr_date / 0x100 ;
  990.         buffer[1] = f_name.wr_date ;
  991.         if( send_( 2 , buffer ) ) {
  992.             if( mode == 2 ) return ;
  993.             timeout() ;
  994.         }
  995.         if( debug ) printf( "ファイル日付送信OK。\n" ) ;
  996.  
  997. /* ファイル時間送信 */
  998.         buffer[0] = f_name.wr_time / 0x100 ;
  999.         buffer[1] = f_name.wr_time ;
  1000.         if( send_( 2 , buffer ) ) {
  1001.             if( mode == 2 ) return ;
  1002.             timeout() ;
  1003.         }
  1004.         if( debug ) printf( "ファイル時間送信OK。\n" ) ;
  1005.  
  1006. /* ファイル属性送信 */
  1007.         buffer[0] = f_name.attrib ;
  1008.         if( send_( 1 , buffer ) ) {
  1009.             if( mode == 2 ) return ;
  1010.             timeout() ;
  1011.         }
  1012.         if( debug ) printf( "ファイル属性送信OK。\n" ) ;
  1013.  
  1014. /* ファイル送信 */
  1015.         printf( " %-13.13s : %10ld " , name , f_len ) ;
  1016.         loop = ( f_len + BUFFSIZE - 1l ) / BUFFSIZE ;
  1017.         for( i = 0 ; i < loop ; i++ ) putch( '.' ) ;
  1018.         for( i = 0 ; i < loop ; i++ ) {
  1019.             len = fread( buffer , 1 , BUFFSIZE , file ) ;
  1020.             if( send_( len , buffer ) ) {
  1021.                 if( mode == 2 ) return ;
  1022.                 timeout() ;
  1023.             }
  1024. /* 受信側ファイル書き込みチェック */
  1025.             if( receive_( 1 , buffer ) ) {
  1026.                 if( mode == 2 ) return ;
  1027.                 timeout() ;
  1028.             }
  1029.             if( buffer[0] == 0x00 ) {
  1030.                 printf( "送信失敗!" ) ;
  1031.                 err = 1 ;
  1032.                 break ;
  1033.             }
  1034.             else printf( "\b \b" ) ;
  1035.         }
  1036.         if( !( err ) ) {
  1037.             ftime( &time_2 ) ;
  1038.             speed = ( f_len_b / ( ( time_2.time - time_1.time ) * 100l
  1039.                   + time_2.millitm - time_1.millitm + 1l ) + 5l ) / 10l ;
  1040.             printf( "(電送速度:約%ldkbps)" , speed ) ;
  1041.         }
  1042.  
  1043. /* ファイルのクローズ */
  1044.         fclose( file ) ;
  1045.  
  1046. /* 受信側ファイルチェック */
  1047.     if( receive_( 1 , buffer ) ) {
  1048.         if( mode == 2 ) return ;
  1049.         timeout() ;
  1050.     }
  1051.     if( buffer[0] == 0x00 ) {
  1052.         printf( "×\n" ) ;
  1053.     }
  1054.     else printf( "○\n" ) ;
  1055.  
  1056. /* 次のファイル展開 */
  1057.         if( ( _dos_findnext( &f_name ) ) != 0 ) {
  1058. /* おしまい */
  1059.             buffer[0] = 0x00 ;
  1060.             if( send_( 1 , buffer ) ) {
  1061.                 if( mode == 2 ) return ;
  1062.                 timeout() ;
  1063.             }
  1064.             break ;
  1065.         }
  1066. /* つづく */
  1067.         else {
  1068.             buffer[0] = 0xff ;
  1069.             if( send_( 1 , buffer ) ) {
  1070.                 if( mode == 2 ) return ;
  1071.                 timeout() ;
  1072.             }
  1073.             if( debug ) printf( "連続送信要求OK。\n" ) ;
  1074.         }
  1075.     }
  1076.  
  1077. }
  1078.  
  1079. /* 受信メイン前(^^;処理 */
  1080. void fp_receive0()
  1081. {
  1082. /* ファイル送信前フラグ受信 */
  1083.     if( receive_( 1 , buffer ) ) timeout() ;
  1084.     if( buffer[0] == 0x02 ) {
  1085.         if( debug ) printf( "ファイル送信前フラグ受信OK。\n" ) ;
  1086.     }
  1087.     else {
  1088.         printf( "ファイル送信前フラグが異常です、送信側コマンドを確認してください。\n" ) ;
  1089.         rs_init() ;
  1090.         exit( -1 ) ;
  1091.     }
  1092. }
  1093.  
  1094. /* 受信メイン処理 */
  1095. void fp_receive()
  1096. {
  1097.     int i , j , len , loop , err ;
  1098.     unsigned long f_len ;
  1099.     unsigned int  f_time , f_date ;
  1100.     unsigned char f_attr ;
  1101.     unsigned char full_name[128] ;
  1102.     FILE *file ;
  1103.     int handle ;
  1104.     unsigned long speed , f_len_b ;
  1105.  
  1106. /* ファイル送信フラグ受信 */
  1107.     if( receive_( 1 , buffer ) ) {
  1108.         if( mode == 2 ) return ;
  1109.         timeout() ;
  1110.     }
  1111.     if( buffer[0] == 0x00 ) {
  1112.         if( debug ) printf( "ファイル送信フラグ受信OK。\n" ) ;
  1113.     }
  1114.     else {
  1115.         printf( "ファイル送信フラグが異常です、送信側コマンドを確認してください。\n" ) ;
  1116.         if( mode == 2 ) return ;
  1117.         rs_init() ;
  1118.         exit( -1 ) ;
  1119.     }
  1120.  
  1121. /* 受信パス名長受信 */
  1122.     if( receive_( 1 , buffer ) ) {
  1123.         if( mode == 2 ) return ;
  1124.         timeout() ;
  1125.     }
  1126.     len = *buffer ;
  1127.     if( debug ) printf( "受信パス名長受信OK。\n" ) ;
  1128. /* 受信パス名受信 */
  1129.     if( len ) {
  1130.         if( receive_( len , buffer ) ) {
  1131.             if( mode == 2 ) return ;
  1132.             timeout() ;
  1133.         }
  1134.         if( ( buffer[len-1] != '\\' ) && ( buffer[len-1] != ':' ) ) {
  1135.             buffer[len] = '\\' ;
  1136.             buffer[len+1] = 0x00 ;
  1137.         }
  1138.         else buffer[len] = 0x00 ;
  1139.         if( debug ) printf( "受信パス名受信OK。\n" ) ;
  1140.         strcpy( path , buffer ) ;
  1141.     }
  1142.     else strcpy( path , "" ) ;
  1143.  
  1144. /* 受信ファイルチェック */
  1145.     if( receive_( 1 , buffer ) ) {
  1146.         if( mode == 2 ) return ;
  1147.         timeout() ;
  1148.     }
  1149.     if( buffer[0] == 0x00 ) {
  1150.         printf( "送信側に送るファイルがありません。\n" ) ;
  1151.         if( mode == 2 ) return ;
  1152.         rs_init() ;
  1153.         exit( -1 ) ;
  1154.     }
  1155.  
  1156. /* 受信処理ループ */
  1157.     if( mode != 2 ) printf( " ファイル名    : ファイル長\n" ) ;
  1158.     for( ; ; ) {
  1159.         ftime( &time_1 ) ;
  1160.         err = 0 ;
  1161.  
  1162. /* 受信ファイルチェック */
  1163.     if( receive_( 1 , buffer ) ) {
  1164.         if( mode == 2 ) return ;
  1165.         timeout() ;
  1166.     }
  1167.     if( buffer[0] == 0x00 ) {
  1168.         printf( "送信側でファイルのオープンに失敗しました。\n" ) ;
  1169.         if( mode == 2 ) return ;
  1170.         rs_init() ;
  1171.         exit( -1 ) ;
  1172.     }
  1173.  
  1174. /* ファイル名長受信 */
  1175.         if( receive_( 1 , buffer ) ) {
  1176.             if( mode == 2 ) return ;
  1177.             timeout() ;
  1178.         }
  1179.         len = *buffer ;
  1180.         if( debug ) printf( "ファイル名長受信OK。\n" ) ;
  1181.  
  1182. /* ファイル名受信 */
  1183.         if( receive_( len , buffer ) ) {
  1184.             if( mode == 2 ) return ;
  1185.             timeout() ;
  1186.         }
  1187.         buffer[len] = 0x00 ;
  1188.         if( debug ) printf( "ファイル名受信OK。\n" ) ;
  1189.         strcpy( name , buffer ) ;
  1190.         strcpy( full_name , path ) ;
  1191.         strcat( full_name , name ) ;
  1192.  
  1193. /* ファイルのオープン */
  1194.         if( ( file = fopen( full_name , "wb" ) ) == NULL ) {
  1195.             printf( "受信ファイルのオープンに失敗しました(%s)。\n" , full_name ) ;
  1196.             buffer[0] = 0x00 ;
  1197.             if( send_( 1 , buffer ) ) {
  1198.                 if( mode == 2 ) return ;
  1199.                 timeout() ;
  1200.             }
  1201.             if( mode == 2 ) return ;
  1202.             rs_init() ;
  1203.             exit( -1 ) ;
  1204.         }
  1205.  
  1206. /* オープン OK */
  1207.     buffer[0] = 0xff ;
  1208.     if( send_( 1 , buffer ) ) {
  1209.         if( mode == 2 ) return ;
  1210.         timeout() ;
  1211.     }
  1212.  
  1213. /* ファイル長受信 */
  1214.         if( receive_( 4 , buffer ) ) {
  1215.             if( mode == 2 ) return ;
  1216.             timeout() ;
  1217.         }
  1218.         f_len = (unsigned long)buffer[0] * 0x1000000l
  1219.             + (unsigned long)buffer[1] * 0x10000l
  1220.             + (unsigned long)buffer[2] * 0x100l
  1221.             + (unsigned long)buffer[3] ;
  1222.         f_len_b = f_len * 8l ;
  1223.         if( debug ) printf( "ファイル長受信OK。\n" ) ;
  1224.  
  1225. /* ファイル日付受信 */
  1226.         if( receive_( 2 , buffer ) ) {
  1227.             if( mode == 2 ) return ;
  1228.             timeout() ;
  1229.         }
  1230.         f_date = (unsigned int)buffer[0] * 0x100
  1231.             + (unsigned int)buffer[1] ;
  1232.         if( debug ) printf( "ファイル日付受信OK。\n" ) ;
  1233.  
  1234. /* ファイル時間受信 */
  1235.         if( receive_( 2 , buffer ) ) {
  1236.             if( mode == 2 ) return ;
  1237.             timeout() ;
  1238.         }
  1239.         f_time = (unsigned int)buffer[0] * 0x100
  1240.             + (unsigned int)buffer[1] ;
  1241.         if( debug ) printf( "ファイル時間受信OK。\n" ) ;
  1242.  
  1243. /* ファイル属性受信 */
  1244.         if( receive_( 1 , buffer ) ) {
  1245.             if( mode == 2 ) return ;
  1246.             timeout() ;
  1247.         }
  1248.         f_attr = buffer[0] ;
  1249.         if( debug ) printf( "ファイル属性受信OK。\n" ) ;
  1250.  
  1251. /* ファイル受信 */
  1252.         printf( " %-13.13s : %10ld " , name , f_len ) ;
  1253.         loop = ( f_len + BUFFSIZE - 1l ) / BUFFSIZE ;
  1254.         for( i = 0 ; i < loop ; i++ ) putch( '.' ) ;
  1255.         for( i = 0 ; i < loop ; i++ ) {
  1256.             if( f_len < BUFFSIZE ) j = f_len ;
  1257.             else j = BUFFSIZE ;
  1258.             if( receive_( j , buffer ) ) {
  1259.                 if( mode == 2 ) return ;
  1260.                 timeout() ;
  1261.             }
  1262.             if( fwrite( buffer , 1 , j    , file ) < j ) {
  1263.                 printf( "書き込み失敗!" ) ;
  1264.                 err = 1 ;
  1265.                 buffer[0] = 0x00 ;
  1266.                 if( send_( 1 , buffer ) ) {
  1267.                     if( mode == 2 ) return ;
  1268.                     timeout() ;
  1269.                 }
  1270.                 break ;
  1271.             }
  1272.             printf( "\b \b" ) ;
  1273.             f_len -= (unsigned long)j ;
  1274.             buffer[0] = 0xff ;
  1275.             if( send_( 1 , buffer ) ) {
  1276.                 if( mode == 2 ) return ;
  1277.                 timeout() ;
  1278.             }
  1279.         }
  1280.         if( !( err ) ) {
  1281.             ftime( &time_2 ) ;
  1282.             speed = ( f_len_b / ( ( time_2.time - time_1.time ) * 100l
  1283.                   + time_2.millitm - time_1.millitm + 1l ) + 5l ) / 10l ;
  1284.             printf( "(電送速度:約%ldkbps)" , speed ) ;
  1285.         }
  1286.  
  1287. /* ファイルのクローズ */
  1288.         fclose( file ) ;
  1289.  
  1290. /* ファイルの日時属性設定 */
  1291.         if( !( err ) ) {
  1292.             if( _dos_open( full_name , O_WRONLY , &handle ) != 0x00 ) {
  1293.                 printf( "受信ファイルのオープンに失敗しました。" ) ;
  1294.                 err = 2 ;
  1295.             }
  1296.             if( _dos_setftime( handle , f_date , f_time ) != 0x00 ) {
  1297.                 printf( "受信ファイルの日時設定に失敗しました。" ) ;
  1298.                 err = 3 ;
  1299.             }
  1300.             if( _dos_setfileattr( full_name , f_attr ) != 0x00 ) {
  1301.                 printf( "送信ファイルの属性設定に失敗しました。" ) ;
  1302.                 err = 3 ;
  1303.             }
  1304.             if( err != 2 ) _dos_close( handle ) ;
  1305.         }
  1306.  
  1307. /* 受信 NG */
  1308.         if( err ) {
  1309.             printf( "×\n" ) ;
  1310.             unlink( full_name ) ;
  1311.             buffer[0] = 0x00 ;
  1312.             if( send_( 1 , buffer ) ) {
  1313.                 if( mode == 2 ) return ;
  1314.                 timeout() ;
  1315.             }
  1316.         }
  1317. /* 受信 OK */
  1318.         else {
  1319.             printf( "○\n" ) ;
  1320.             buffer[0] = 0xff ;
  1321.             if( send_( 1 , buffer ) ) {
  1322.                 if( mode == 2 ) return ;
  1323.                 timeout() ;
  1324.             }
  1325.         }
  1326.  
  1327. /* 次のファイルチェック */
  1328.         if( receive_( 1 , buffer ) ) {
  1329.             if( mode == 2 ) return ;
  1330.             timeout() ;
  1331.         }
  1332.         if( buffer[0] == 0x00 ) break ;
  1333.         if( debug ) printf( "連続受信要求OK。\n" ) ;
  1334.  
  1335.     }
  1336.  
  1337. }
  1338.  
  1339. /* サーバーメイン処理 */
  1340. void fp_server()
  1341. {
  1342.     int len ;
  1343.     unsigned long f_len ;
  1344.     int handle ;
  1345.     FILE *file ;
  1346.  
  1347. /* ESCが来るまで無限ループ */
  1348.     printf( " ファイル名    : ファイル長\n" ) ;
  1349.     for( ; ; ) {
  1350.         strcpy( path , "" ) ;
  1351.         while( connect_r() ) ;
  1352.  
  1353. /* ファイル送信フラグ受信 */
  1354.         if( receive_( 1 , buffer ) ) buffer[0] = 0xff ;
  1355.         switch( buffer[0] ) {
  1356.             case 0x01 :            /* サーバーから送信 */
  1357. /* 送信ファイル名長受信 */
  1358.                 if( receive_( 1 , buffer ) ) break ;
  1359.                 len = *buffer ;
  1360.                 if( debug ) printf( "送信ファイル名長受信OK。\n" ) ;
  1361. /* 送信ファイル名受信 */
  1362.                 if( receive_( len , buffer ) ) break ;
  1363.                 buffer[len] = 0x00 ;
  1364.                 if( debug ) printf( "送信ファイル名受信OK。\n" ) ;
  1365.                 strcpy( name , buffer ) ;
  1366.                 fp_send() ;
  1367.                 break ;
  1368.             case 0x02 :            /* サーバーに受信    */
  1369.                 fp_receive() ;
  1370.                 break ;
  1371.             case 0x03 :            /* リモートコマンド処理 */
  1372. /* リモートコマンド長受信 */
  1373.                 if( receive_( 1 , buffer ) ) break ;
  1374.                 len = *buffer ;
  1375.                 if( debug ) printf( "リモートコマンド長受信OK。\n" ) ;
  1376. /* リモートコマンド受信 */
  1377.                 if( receive_( len , command ) ) break ;
  1378.                 command[len] = 0x00 ;
  1379.                 if( debug ) printf( "リモートコマンド受信OK。\n" ) ;
  1380. /* リモートコマンド実行 */
  1381.                 strcat( command , ">$$fpli$$.tmp" ) ;
  1382.                 system( command ) ;
  1383. /* ファイルの長さを調べる _dos_finedではDIETしてあるデータの長さが分からない */
  1384.                 strcpy( name , "$$fpli$$.tmp" ) ;
  1385.                 if( ( handle = open( name , O_RDONLY|O_BINARY ) ) == EOF ) {
  1386.                     f_len = 0l ;
  1387.                 }
  1388.                 else {
  1389.                     f_len = filelength( handle ) ;
  1390.                     close( handle ) ;
  1391.                 }
  1392. /* コマンド結果ファイルのオープン */
  1393.                 file = fopen( name , "rb" ) ;
  1394. /* コマンド結果ファイル長送信 */
  1395.                 buffer[0] = f_len / 0x1000000l ;
  1396.                 buffer[1] = ( f_len & 0x00ff0000l ) / 0x10000l ;
  1397.                 buffer[2] = ( f_len & 0x0000ff00l ) / 0x100l ;
  1398.                 buffer[3] = f_len & 0x000000ffl ;
  1399.                 if( send_( 4 , buffer ) ) break ;
  1400.                 if( debug ) printf( "コマンド結果ファイル長送信OK。\n" ) ;
  1401. /* コマンド結果ファイル送信 */
  1402.                 for( ; f_len > 0l ; ) {
  1403.                     len = fread( buffer , 1 , BUFFSIZE , file ) ;
  1404.                     if( send_( len , buffer ) ) break ;
  1405.                     f_len -= (unsigned long)len ;
  1406.                 }
  1407.                 if( debug ) printf( "コマンド結果ファイル送信OK。\n" ) ;
  1408.                 fclose( file ) ;
  1409.                 unlink( name ) ;
  1410.                 break ;
  1411.             default :
  1412.                 printf( "ファイル送信フラグが異常です、送信側コマンドを確認してください。\n" ) ;
  1413.                 break ;
  1414.         }
  1415.     if( debug ) printf( "サーバー処理1シーケンス完了\n" ) ;
  1416.     }
  1417.  
  1418. }
  1419.  
  1420. /* サーバーからの受信処理用(^^;ファイル名送信処理 */
  1421. void fp_file()
  1422. {
  1423.     int len ;
  1424.  
  1425. /* ファイル名送信フラグ送信(^^; */
  1426.     buffer[0] = 0x01 ;
  1427.     if( send_( 1 , buffer ) ) timeout() ;
  1428.     if( debug ) printf( "ファイル名送信フラグ送信OK。\n" ) ;
  1429.  
  1430. /* 受信ファイル名長送信 */
  1431.     len = strlen( name ) ;
  1432.     *buffer = (unsigned char)len ;
  1433.     if( send_( 1 , buffer ) ) timeout() ;
  1434.     if( debug ) printf( "受信ファイル名長送信OK。\n" ) ;
  1435.  
  1436. /* 受信ファイル名送信 */
  1437.     if( send_( len , name ) ) timeout() ;
  1438.     if( debug ) printf( "受信ファイル名送信OK。\n" ) ;
  1439.  
  1440. }
  1441.  
  1442. /* リモートコマンド処理 */
  1443. void fp_command()
  1444. {
  1445.     int len , i , j;
  1446.     unsigned long f_len ;
  1447.     unsigned char *c ;
  1448.  
  1449. /* リモートコマンド送信フラグ送信(^^; */
  1450.     buffer[0] = 0x03 ;
  1451.     if( send_( 1 , buffer ) ) timeout() ;
  1452.     if( debug ) printf( "リモートコマンド送信フラグ送信OK。\n" ) ;
  1453.  
  1454. /* リモートコマンド長送信 */
  1455.     len = strlen( command ) ;
  1456.     *buffer = (unsigned char)len ;
  1457.     if( send_( 1 , buffer ) ) timeout() ;
  1458.     if( debug ) printf( "リモートコマンド長送信OK。\n" ) ;
  1459.  
  1460. /* リモートコマンド送信 */
  1461.     if( send_( len , command ) ) timeout() ;
  1462.     if( debug ) printf( "受信先パス名送信OK(%s)。\n" , command ) ;
  1463.  
  1464. /* コマンド結果長受信 */
  1465.     while( receive_( 4 , buffer ) ) ;
  1466.     f_len = (unsigned long)buffer[0] * 0x1000000l
  1467.         + (unsigned long)buffer[1] * 0x10000l
  1468.         + (unsigned long)buffer[2] * 0x100l
  1469.         + (unsigned long)buffer[3] ;
  1470.     if( debug ) printf( "コマンド結果長受信OK。\n" ) ;
  1471.  
  1472. /* コマンド結果受信&表示 */
  1473.     for( ; f_len > 0l ; ) {
  1474.         if( f_len < BUFFSIZE ) i = f_len ;
  1475.         else i = BUFFSIZE ;
  1476.         if( receive_( i , buffer ) ) timeout() ;
  1477.         c = buffer ;
  1478.         for( j = 0 ; j < i ; j++ ) {
  1479.             putch( *c++ ) ;
  1480.         }
  1481.         f_len -= (unsigned long)i ;
  1482.     }
  1483.     if( debug ) printf( "コマンド結果受信OK。\n" ) ;
  1484.  
  1485. }
  1486.  
  1487. /* タイムアウト停止 */
  1488. void timeout()
  1489. {
  1490.     printf( "タイムアウトが発生しました(;_;)。\n" ) ;
  1491.     rs_init() ;
  1492.     exit( -1 ) ;
  1493.  
  1494. }
  1495.  
  1496. /* 割り込み処理定義 */
  1497. void sig_out()
  1498. {
  1499.  
  1500.     printf( "電送処理終了します。\n" ) ;
  1501.     rs_init() ;
  1502.     exit( -1 ) ;
  1503.  
  1504. }
  1505.  
  1506. /* タイムアウト値取得処理 */
  1507. void timeset()
  1508. {
  1509.     long time_0,time_1,time_2 ;
  1510.  
  1511.     time_0 = clock() ;
  1512.     time_1 = time_0 + 1l ;
  1513.     time_2 = time_0 + 2l ;
  1514.  
  1515.     while( clock() != time_1 ) ;
  1516.     for( timeout1 = 0l ; timeout1 < 2000000000l ; timeout1 += 2500l ) {
  1517.         if( clock() == time_2 ) break ;
  1518.     }
  1519.  
  1520.     if( debug ) printf( "タイムアウト値は(%ld)です。\n" , timeout1 ) ; 
  1521.  
  1522. }
  1523.  
  1524.